﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PhotoOrganizer
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
            updateStatus = new FileHandler.DelUpdateStatus(UpdateStatus);
        }

        private FileHandler.DelUpdateStatus updateStatus = null;
        private System.Threading.Thread thread = null;

        private void btnOrganize_Click(object sender, EventArgs e)
        {

            thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(FileHandler.OrganizeFiles));
            thread.Start((object)new object[] { txtDestination.Text, updateStatus });
        }

        public void UpdateStatus(int pTotal, int pCurrent)
        {
            if (InvokeRequired)
            {
                this.Invoke(new FileHandler.DelUpdateStatus(UpdateStatus), pTotal, pCurrent);
            }
            else
            {
                progressBar1.Maximum = pTotal;
                progressBar1.Value = pCurrent % pTotal;
            }
        }

        private void btnGatherFileInfo_Click(object sender, EventArgs e)
        {
            thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(FileHandler.GatherFileInfo));
            thread.Start((object)new object[] { chkSourcePaths.CheckedItems.Cast<string>().ToList(), chkExtensions.CheckedItems.Cast<string>().ToList(), updateStatus });
        }

        private void btnFindDuplicates_Click(object sender, EventArgs e)
        {
            thread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(FileHandler.GatherFileInfo));
            thread.Start((object)new object[] { chkSourcePaths.CheckedItems.Cast<string>().ToList(), chkExtensions.CheckedItems.Cast<string>().ToList(), updateStatus });
        }

        private void txtExtensions_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter && !chkExtensions.Items.Contains(txtExtensions.Text))
            {
                chkExtensions.Items.Add(txtExtensions.Text);
            }
        }

        private void txtSourcePath_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter && !chkSourcePaths.Items.Contains(txtSourcePath.Text))
            {
                chkSourcePaths.Items.Add(txtSourcePath.Text);
            }
        }

        private void chkExtensions_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete && chkExtensions.SelectedIndex > -1)
            {
                chkExtensions.Items.RemoveAt(chkExtensions.SelectedIndex);
            }
        }

        private void chkSourcePaths_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Delete && chkSourcePaths.SelectedIndex > -1)
            {
                chkSourcePaths.Items.RemoveAt(chkSourcePaths.SelectedIndex);
            }
        }

        private void btnFindDuplicates_Click_1(object sender, EventArgs e)
        {
            FileHandler.FindDuplicates();
        }
    }
}
